﻿﻿// Copyright (c) 2010. Rusanu Consulting LLC  
// http://code.google.com/p/linqtocache/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.Objects;

namespace System.Linq
{
    ///// <summary>
    ///// Extensions for IQueryable&lt;T&gt;
    ///// </summary>
    //public static class Category
    //{

    //    /// <summary>
    //    /// The cache key is built from the query itself.
    //    /// <para>Transforms an arbitrary IQueryable&lt;T&gt; interface into a potentially cached IEnumerable&lt;T&gt;.</para>
    //    /// <para>The cached result is dependent on a SqlDependency notification and SQL Server will invalidate it when
    //    /// the returned result is changed by any server side operation.</para>
    //    /// <para>Note that not all queries can be notified. To be valid for active notifications, a query must satisfy
    //    /// the restrictions outlined at Creating a Query for Notification: http://msdn.microsoft.com/en-us/library/ms181122.aspx </para>
    //    /// <para>Queries that don't satisfy the conditions will be invalidated immedeatly and evicted from the cache.</para>
    //    /// </summary>
    //    /// <param name="options">Optional caching options. Use this object to get information about the source
    //    /// of the query result (FromCache or FromQuery) </param>
    //    /// <param name="cacheKey">Optionally specify a custom cache lookup key. By default a custom MD5 cache key will be generated from the query.</param>
    //    /// <typeparam name="T">The type being returned by the query</typeparam>
    //    /// <param name="query">The IQueryable that is being extended with this method</param>
    //    /// <param name="preventCache">Specify true to prevent cache ie. when you want to save objects in db</param>
    //    /// <param name="onInvalidation">
    //    /// Event raised when the query is invalidated by the server.
    //    /// Only the CachedQueryOptions for a result returned from the query raise this event.
    //    /// </param>
    //    /// <returns>The query result as an IEnumerable&lt;T&gt;</returns>
    //    /// <created author="laurentiu.macovei" date="Sat, 03 Mar 2012 01:16:07 GMT"/>
    //    [DebuggerStepThrough]
    //    public static IEnumerable<T> AsCached<T>(this IQueryable<T> query, bool preventCache = false, Action<CachedQueryEventArgs> onInvalidation = null, string cacheKey = null, CachedQueryOptions options = null)
    //    {
    //        if (options != null)
    //        {
    //            if (onInvalidation != null)
    //            {
    //                var handler = options.OnInvalidated;
    //                options.OnInvalidated = (s, o) =>
    //                {
    //                    if (handler != null)
    //                        handler(s, o);
    //                    onInvalidation(o);
    //                };
    //            }
    //        }
    //        cacheKey = cacheKey ?? CodeSmith.Data.Linq.QueryResultCache.GetHashKey(query);
    //        var Options = options ?? new CachedQueryOptions { OnInvalidated = onInvalidation == null ? (EventHandler<CachedQueryEventArgs>)null : (o, a) => onInvalidation(a) };
    //        if (preventCache)
    //        {
    //            CachedQuery<T>.Remove(cacheKey);
    //            if (Options.OnInvalidated != null)
    //                Options.OnInvalidated(null, new CachedQueryEventArgs
    //                {
    //                    CacheKey = cacheKey,
    //                    NotificationEventArgs = new Data.SqlClient.SqlNotificationEventArgs(
    //                        Data.SqlClient.SqlNotificationType.Unknown,
    //                        Data.SqlClient.SqlNotificationInfo.PreviousFire,
    //                        Data.SqlClient.SqlNotificationSource.Client),
    //                    Tag = Options.Tag
    //                });
    //            return query;
    //        }
    //        return new CachedQuery<T> { Query = query, Key = cacheKey, Options = Options };
    //    }



    //}
}
